package com.dhecp.project.hr.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.utils.MessageUtils;
import com.dhecp.common.utils.RandomValueUtils;
import com.dhecp.core.constant.DataFiltering;
import com.dhecp.core.utils.PageUtils;
import com.dhecp.framework.util.ShiroUtils;
import com.dhecp.project.common.service.impl.BaseModuleServiceImpl;
import com.dhecp.project.hr.domain.AmazonAnalysis;
import com.dhecp.project.hr.domain.AmazonShopProductReport;
import com.dhecp.project.hr.mapper.AmazonAnalysisMapper;
import com.dhecp.project.hr.service.AmazonAnalysisService;
import com.dhecp.project.system.service.SysDictionaryForFieldService;

/**
 * @program: kjecp
 * @description: 亚马逊商品运营分析表
 * @author: hlf
 * @create: 2022-10-13 16:01
 */
@Service
public class AmazonAnalysisServiceImpl extends BaseModuleServiceImpl implements AmazonAnalysisService {

    @Autowired
    private AmazonAnalysisMapper amazonAnalysisMapper;

    @Autowired
    private SysDictionaryForFieldService dictionaryForFieldService;


    /**
     * 查询
     * @param params
     * @return List - AttendanceQJ
     * @throws Exception
     * @author DH-Xxj
     */
    @Override
    public List<AmazonAnalysis> queryList(Map<String, Object> params) throws Exception {
        params.put(DataFiltering.TABLE_ALIAS, "b");	//查询表别名
        params.put(DataFiltering.DATA_WHERE, dictionaryForFieldService.getWhereStr(params));	//常规查询条件SQL语句
        params.put(DataFiltering.DATA_SCODE, dictionaryForFieldService.getDataScode(params));	//数据权限查询条件SQL语句
        PageUtils.startPage();
        return amazonAnalysisMapper.selectAmazonAnalysis(params);
    }


    /**
     * 导入
     * @param moduleTable 必要参数：功能唯一标识
     * @param infoItems 必要参数：对应实体类的json格式字符串（导入的字段用户内容）
     * @return AjaxResult
     * @author DH-Xxj
     */
    @Override
    public AjaxResult saveImportData(Map<String, Object> params){

        List<AmazonShopProductReport> amazonShopProductReportList = JSON.parseArray(params.get("infoItems").toString(), AmazonShopProductReport.class);
        if (amazonShopProductReportList == null || amazonShopProductReportList.size() == 0) {
            return AjaxResult.error("数据包格式有误，请检查！");
        }

        for (AmazonShopProductReport item : amazonShopProductReportList) {

            Map<String, Object> params2 = new HashMap<String, Object>();
            params2.put("dataDate",item.getDataDate());
            params2.put("sellerSku",item.getSellerSku());
            AmazonShopProductReport amazonShopProductReport =  amazonAnalysisMapper.findBySku(params2);

            if (amazonShopProductReport!=null){
                item.setGuid(RandomValueUtils.getGUID());
                item.setCreateTime(new Date());
                item.setCreateUser(ShiroUtils.getUserId());
                amazonAnalysisMapper.importInsert(item);
            }else{
                item.setUpdateTime(new Date());
                item.setUpdateUser(ShiroUtils.getUserId());
                amazonAnalysisMapper.importUpdate(item);
            }

        }
        return AjaxResult.success(MessageUtils.message("module.success.issuccess"));
    }
}
